import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class customRobotGroupMessageMySelf {

    public static final String CUSTOM_ROBOT_TOKEN = "7c5a2ddbabb1ed8d9147dd5c1e5e7df2384db341d917e914349527f6db9fe0a1";

    public static final String USER_ID= "13168693619";

    public static final String SECRET = "SEC504b3f14e23b6d923dc36c721282915415ecbb069db4cbdec32de8d7c43ec352";

    public static void main(String[] args) {
        try {
            Long timestamp = System.currentTimeMillis();
            System.out.println(timestamp);
            String secret = SECRET;
            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
            System.out.println(sign);

            //sign字段和timestamp字段必须拼接到请求URL上，否则会出现 310000 的错误信息
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign="+sign+"&timestamp="+timestamp);
            OapiRobotSendRequest req = new OapiRobotSendRequest();
            /**
             * https://oapi.dingtalk.com/robot/send?access_token=7c5a2ddbabb1ed8d9147dd5c1e5e7df2384db341d917e914349527f6db9fe0a1
             * SEC504b3f14e23b6d923dc36c721282915415ecbb069db4cbdec32de8d7c43ec352
             * 发送文本消息
             */
            //定义文本内容
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("钉钉，让进步发生");
            //定义 @ 对象
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtUserIds(Arrays.asList(USER_ID));
            //设置消息类型
//            req.setMsgtype("text");
//            req.setText(text);

//            //图片
//            req.setMsgtype("link");
//            OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
//            link.setText("");
//            link.setTitle("");
//            link.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1XdXXXXyVXa-2400-1200.png");
//            link.setMessageUrl("");
////            link.setMessageUrl("https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
//            req.setLink(link);

            //mardown
            req.setMsgtype("markdown");
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setText("####  ![](https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png) \n");
                    markdown.setTitle("杭州天气");
                    req.setMarkdown(markdown);


            req.setAt(at);
            OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }
}
